package org.openkoala.demo.web.controller;

import java.util.HashMap;
import java.util.Map;
import javax.inject.Inject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.PathVariable;
import org.dayatang.querychannel.Page;
import org.openkoala.demo.facade.dto.*;
import org.openkoala.demo.facade.UserFacade;
import org.openkoala.koala.commons.InvokeResult;

@Controller
@RequestMapping("/User")
public class UserController {
		
	@Inject
	private UserFacade userFacade;
	
	@ResponseBody
	@RequestMapping("/add")
	public InvokeResult add(UserDTO userDTO) {
		return userFacade.saveUser(userDTO);
	}
	
	@ResponseBody
	@RequestMapping("/update")
	public InvokeResult update(UserDTO userDTO) {
		return userFacade.updateUser(userDTO);
	}
	
	@ResponseBody
	@RequestMapping("/pageJson")
	public Page pageJson(UserDTO userDTO, @RequestParam int page, @RequestParam int pagesize) {
		Page<UserDTO> all = userFacade.pageQueryUser(userDTO, page, pagesize);
		return all;
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public InvokeResult delete(@RequestParam String ids) {
		String[] value = ids.split(",");
        Long[] idArrs = new Long[value.length];
        for (int i = 0; i < value.length; i ++) {
        	        					idArrs[i] = Long.parseLong(value[i]);
						        }
        return userFacade.removeUsers(idArrs);
	}
	
	@ResponseBody
	@RequestMapping("/get/{id}")
	public InvokeResult get(@PathVariable Long id) {
		return userFacade.getUser(id);
	}
	
		
}
